package
{
	import fl.controls.NumericStepper;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.system.Security;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	import widgets.BottomBar;
	import widgets.PreviewArea;
	import widgets.PreviewAreaMask;
	import widgets.Tip;
	import widgets.VideoScreen;
	
	
	
	[SWF(width="320",height="240")]
	public class ScreenShot extends Sprite
	{
		private var previewButton:PreviewButton;
		private var fileButton:FileButton;
		private var preview:PreviewArea;
		private var premask:PreviewAreaMask;
		private var camButton:CamButton;
		private var _parent:MovieClip;
		private var bottomBar:BottomBar;
		private var uploadButton:UploadButton;
		private var tip:Tip;
		
		private var loader:Loader;
		
		private var pic:Bitmap;
		private var vod:Video;
		private var currentFlag:String = "pic";
		
		private var bgMc:MovieClip;
		
		private var file:FileReference;
		private var fileType:FileFilter;
		
		public function ScreenShot()
		{
			
			this.bgMc = new MovieClip();
			this.addChild(bgMc);
			
			preview = new PreviewArea();
			preview.x = (320 - preview.width)/2;
			preview.y = (240 - preview.height)/2 - 30;
			preview.mouseEnabled = false;
			this.addChild(preview);
			preview.visible = false;
			
			premask = new PreviewAreaMask();
			premask._width = 320;
			premask._height = 240;
			premask.DrawGraphics(preview.x,preview.y,preview.x + preview.width, preview.y + preview.height);
			//premask.visible = false;
			this.addChild(premask);
			
			fileButton = new FileButton();
			fileButton.x = 83 ;
			fileButton.y = 155;
			fileButton.buttonMode = true;
			fileButton.useHandCursor = true;
			fileButton.addEventListener(MouseEvent.CLICK,fileButtonClickHandler);
			fileButton.addEventListener(MouseEvent.MOUSE_OVER,buttonMouseOverHandler);
			fileButton.addEventListener(MouseEvent.MOUSE_OUT,buttonMouseOutHandler);
			this.addChildAt(fileButton,this.getChildIndex(premask)+1);
			
			camButton = new CamButton();
			camButton.x = 138;
			camButton.y = 155;
			camButton.buttonMode = true;
			camButton.useHandCursor = true;
			camButton.addEventListener(MouseEvent.CLICK,CamButtonClickHandler);
			camButton.addEventListener(MouseEvent.MOUSE_OVER,buttonMouseOverHandler);
			camButton.addEventListener(MouseEvent.MOUSE_OUT,buttonMouseOutHandler);
			this.addChildAt(camButton,this.getChildIndex(premask)+1);
			
			previewButton = new PreviewButton();
			previewButton.x = 193;
			previewButton.y = 155;
			previewButton.buttonMode = true;
			previewButton.useHandCursor = true;
			previewButton.addEventListener(MouseEvent.CLICK,previewButtonClickHandler);
			previewButton.addEventListener(MouseEvent.MOUSE_OVER,buttonMouseOverHandler);
			previewButton.addEventListener(MouseEvent.MOUSE_OUT,buttonMouseOutHandler);
			this.addChildAt(previewButton,this.getChildIndex(premask)+1);
			
			bottomBar = new BottomBar();
			bottomBar.x = 0;
			bottomBar.y = 200;
			bottomBar.mouseEnabled = false;
			//this.addChild(bottomBar);
			this.addChildAt(bottomBar,this.getChildIndex(fileButton)+1);
			
			uploadButton = new UploadButton();
			uploadButton.label.mouseEnabled = false;
			uploadButton.buttonMode = true;
			uploadButton.useHandCursor = true;
			uploadButton.x = (bottomBar.width - uploadButton.width)/2;
			uploadButton.y = (bottomBar.height - uploadButton.height)/2;
//			uploadButton.x = (320- uploadButton.width) / 2;
//			uploadButton.y = 240-  uploadButton.height - (bottomBar.height-uploadButton.height)/2 ;
			uploadButton.addEventListener(MouseEvent.MOUSE_OVER,buttonMouseOverHandler);
			//uploadButton.addEventListener(MouseEvent.MOUSE_MOVE,buttonMouseOverHandler);
			uploadButton.addEventListener(MouseEvent.MOUSE_OUT,buttonMouseOutHandler);
//			this.addChildAt(uploadButton,this.getChildIndex(bottomBar)+1);
			this.bottomBar.addChild(uploadButton);
			
			uploadButton.enabled = false;
			
			tip = new Tip();
			tip.x = 20;
			tip.y = 20;
			this.addChildAt(tip,this.getChildIndex(premask)+1);
			tip.visible = false;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			
		}
		
		private function buttonMouseOverHandler(event:MouseEvent):void
		{
			this.tip.visible = true;
			
			switch(event.target.toString())
			{
				case "[object FileButton]":
					tip.x = event.target.x - tip.width/2 + event.target.width/2;
					tip.y = event.target.y -30 ;
					tip.SetText("使用本地图像");
					break;
				case "[object CamButton]":
					tip.x = event.target.x - tip.width/2 + event.target.width/2;
					tip.y = event.target.y -30 ;
					tip.SetText("使用摄像头拍照");
					break;
				case "[object PreviewButton]":
					tip.x = event.target.x - tip.width/2 + event.target.width/2;
					tip.y = event.target.y -30 ;
					tip.SetText("预览修改效果");
					break;
				case "[object UploadButton]":
					tip.x = (320- event.target.width) / 2 - tip.width/2 + event.target.width/2;
					tip.y = 125;
					tip.SetText("上传修改完的头像");
					break;
			}
		
		}
		private function buttonMouseOutHandler(event:MouseEvent):void
		{
			this.tip.visible = false;
		}
		
		private function previewButtonClickHandler(event:MouseEvent):void
		{
			var bmd:BitmapData = new BitmapData(preview.width-4,preview.height-4,true);
			var rect:Rectangle = new Rectangle(0,0,preview.width,preview.height);
			if(this.loader !=null)
			{
				var picMatrix:Matrix;
				
				if(this.bgMc.x == 0 )
				{
					picMatrix = new Matrix(this.loader.scaleX,0,0,this.loader.scaleY,this.loader.x- this.preview.x ,this.loader.y-this.preview.y);  
				}else	
				{
					picMatrix = new Matrix(this.loader.scaleX,0,0,this.loader.scaleY,this.loader.x - this.preview.x + this.bgMc.x ,this.loader.y - this.preview.y + this.bgMc.y);
				}
			}
			var vodMatrix:Matrix = new Matrix(this.bgMc.scaleX,0,0,this.bgMc.scaleY,this.bgMc.x-this.preview.x,this.bgMc.y-this.preview.y);
			switch(this.currentFlag)
			{
				case "pic":
					if(this.pic !=null)
					{
						bmd.draw(this.pic,picMatrix,null,null,rect,true);
					}
					break;
				case "vod":
					if(this.vod !=null)
					{
						bmd.draw(this.vod,vodMatrix,null,null,rect,true);
					}
					break;
			}
			var bmp:Bitmap = new Bitmap(bmd);
			this.preview.visible = true;
			this.premask.visible = false;
			bmp.x = 1;
			bmp.y = 1;
			this.preview.stage.quality =StageQuality.BEST;
			this.preview.addChild(bmp);
			this.bgMc.visible = false;
			this.uploadButton.enabled = true;
			this.uploadButton.label.setTextFormat(new TextFormat(null,null,0x000000));
			

		}
		
		private function fileButtonClickHandler(event:MouseEvent):void
		{
			this.preview.visible = false;
			this.premask.visible = true;
			this.file = new FileReference();
			this.fileType = new FileFilter("图片文件(*.jpg;*.jpeg;*.png)","*.jpg;*.jpeg;*.png");
			this.file.addEventListener(Event.SELECT,FileSelectHandler);
			this.file.addEventListener(Event.CANCEL,FileCancelHandler);
			this.file.browse([fileType]);
		}
		private function FileCompleteHandler(event:Event):void
		{
			var bytes:ByteArray = ByteArray(event.target.data);
			this.loader = new Loader();			
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
			this.loader.loadBytes(bytes);
		}
		private function loaderCompleteHandler(event:Event):void
		{
			this.removeChild(this.bgMc);
			this.bgMc = new MovieClip();
			this.bgMc.x = this.bgMc.y = 0;
			this.bgMc.scaleX = this.bgMc.scaleY = 1;
			this.bgMc.addChild(event.target.loader);
			resize(event.target.loader);
			this.addChildAt(bgMc,0);
			this.pic = event.target.content;
			this.currentFlag = "pic";
			this.bgMc.addEventListener(MouseEvent.MOUSE_DOWN, bgMcStartDragHandler);
			this.bgMc.addEventListener(MouseEvent.MOUSE_UP,bgMcStopDragHandler);
			this.bgMc.addEventListener(MouseEvent.MOUSE_WHEEL,bgMcWheelHandler);	
		}
		
		private function resize(loader:Loader):void
		{
			var w4:Number = 100;
			var h4:Number = 100;
			var w:Number = 320;
			var h:Number = 200;
			var a:Number = w/h;
			var w1:Number = loader.width;
			var h1:Number = loader.height;
			var a1:Number = w1/h1;
			var w2:Number;
			var h2:Number;
			if(a1>a)
			{
				w2 = w;
				h2 = w/a1;
			}else if(a1<a)
			{
				h2 = h;
				w2 = h * a1;
			}else if(a1==a)
			{
				w2 = w;
				h2 = h;
			}
			if(loader.width < w && loader.height < h)
			{
				if(loader.width == loader.height)
				{
					loader.width = w4;
					loader.height = h4;
				}
				else
				{
					loader.width = w1;
					loader.height = h1;
				}
				
			}
			else
			{
				loader.width = w2;
				loader.height = h2;
			}
			loader.x = (w - loader.width)/2;
			loader.y = (h - loader.height)/2-10;
		}
		
		private function bgMcStartDragHandler(event:MouseEvent):void
		{
			this.bgMc.startDrag(false,null);
		}
		
		private function bgMcStopDragHandler(event:MouseEvent):void
		{
			this.bgMc.stopDrag();
		}
		private function bgMcWheelHandler(event:MouseEvent):void
		{
			if(event.delta >0)
			{
				this.loader.scaleX += 0.1;
				this.loader.scaleY += 0.1;
			}else if(event.delta <0)
			{
				this.loader.scaleX -= 0.1;
				this.loader.scaleY -= 0.1;
			}
		}
		
		private function FileSelectHandler(event:Event):void
		{
			this.file.load();
			this.file.addEventListener(Event.COMPLETE,FileCompleteHandler);
			
		}
		private function FileCancelHandler(event:Event):void
		{
			
		}
		
		private function CamButtonClickHandler(event:MouseEvent):void
		{
			this.preview.visible = false;
			this.premask.visible = true;
			this.removeChild(bgMc);
			this.bgMc = new MovieClip();	
			this.bgMc.x = this.bgMc.y = 0;
			this.bgMc.scaleX = this.bgMc.scaleY = 1;
			var vs:VideoScreen = new VideoScreen();
			this.bgMc.addChild(vs);
			this.addChildAt(bgMc,0);
			this.vod = vs.GetVideo();
			this.currentFlag = "vod";
			this.bgMc.addEventListener(MouseEvent.MOUSE_DOWN, bgMcStartDragHandler);
			this.bgMc.addEventListener(MouseEvent.MOUSE_UP,bgMcStopDragHandler);
			this.bgMc.addEventListener(MouseEvent.MOUSE_WHEEL,bgMcWheelHandler);
			
		}
	}
}